﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using Newtonsoft.Json;
using System.Data;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 评议人定义
    /// </summary>
    /// 
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class PeerReviewVoterController : AdminBaseController
    {
        /// <summary>
        /// 人员逻辑类
        /// </summary>
        private BLL.HREmpl hrEmplBLL = new BLL.HREmpl();
        //
        // GET: /Admin/PeerReviewVoter/
        /// <summary>
        /// 评议人定义主页
        /// </summary>
        /// <returns>视图</returns>
        /// 
        [AuthorityActionFilter("m_PeerReviewVoter", Order = 1)]
        [LogPageActionFilter("评议人定义", Order = 2)]
        public ActionResult Index()
        {

            BLL.PeerReview peerReviewBll = new BLL.PeerReview();

            DataSet ds = peerReviewBll.GetIsFtatus();

            string isFstatus = "";
            if (ds.Tables[0].Rows.Count > 0)
            {
                isFstatus = ds.Tables[0].Rows[0]["FStatus"].ToString();
            }
            else
            {
                isFstatus = "";
            }
            ViewData["isFstatus"] = isFstatus;

            return View();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ActionResult PeerReviewVoterContent() 
        {
            BLL.PeerReview peerReviewBll = new BLL.PeerReview();
            List<Model.PeerReview> peerReviewList = peerReviewBll.GetList();//只有1个主题
            foreach (Model.PeerReview model in peerReviewList)
            {
                ViewData["FBeenVoterClsName"] = model.FTitle;
                ViewData["peerReviewID"] = model.FPeerReviewID;
            }
            ViewData["list"] = peerReviewList;

            ViewData["tableJsonStr"] = GetTableJsonStr();

            BLL.PeerReviewResult peerReviewResultBll = new BLL.PeerReviewResult();
            DataSet ds = peerReviewResultBll.GetAllList();
            string visible = "false";
            if (ds.Tables[0].Rows.Count > 0)
            {
                visible = "true";
            }
            ViewData["visible"] = visible;

            return View();
        
        }

        /// <summary>
        /// 得到部门和人员
        /// </summary>
        /// <param name="returnValue">树选择的人员</param>
        /// <param name="peerReviewID">主题ID</param>
        /// <returns>字符串</returns>
        public string GetTableJsonStr(string returnValue = "", string peerReviewID = "")
        {
            string tableJsonStr = "";
            string entityNo = GlobalConstant.ENTITYNO;
            BLL.PeerReviewVoter peerReviewVoterBll = new BLL.PeerReviewVoter();
            List<Model.PeerReviewVoter> peerReviewVoterList = peerReviewVoterBll.GetListByEntityNo(entityNo);
            tableJsonStr += "[";
            if (returnValue == "")
            {
                if (peerReviewVoterList.Count > 0)
                {
                    foreach (Model.PeerReviewVoter model in peerReviewVoterList)
                    {
                        tableJsonStr += "{";
                        tableJsonStr += "\"FEmpName\":\"" + model.FEmpName + "\",\"FEmpNo\":\"" + model.FVoterEmplNo + "\",\"FDeptName\":\"" + model.deptName + "\",\"FDeptNo\":\"" + model.FVoterDeptNo + "\",\"Value\":\"" + model.FVoterEmplNo + "\"";
                        tableJsonStr += "},";
                    }
                    tableJsonStr = tableJsonStr.Substring(0, tableJsonStr.LastIndexOf(","));
                }
            }
            //选择的人员
            else if (returnValue != "")
            {

                List<SelectListItem> itemList = JsonConvert.DeserializeObject<List<SelectListItem>>(returnValue);
                List<Model.PeerReviewVoter> peerReviewVotorModelList = new List<Model.PeerReviewVoter>();
                Model.PeerReviewVoter peerReviewVoterModel = new Model.PeerReviewVoter();
                BLL.PeerReviewBeenVoter peerReviewBeenVoterBll = new BLL.PeerReviewBeenVoter();

                foreach (SelectListItem selectItem in itemList)
                {

                    peerReviewVoterModel = new Model.PeerReviewVoter();
                    peerReviewVoterModel.FVoterDeptNo = peerReviewBeenVoterBll.GetDeptNoByEmpNo(selectItem.Value);
                    peerReviewVoterModel.FVoterEmplNo = selectItem.Value;
                    peerReviewVoterModel.FPeerReviewID = decimal.Parse(peerReviewID);
                    peerReviewVoterModel.FEntityNo = entityNo;
                    peerReviewVoterModel.FStatus = "N";
                    peerReviewVoterModel.FVoteDate = null;
                    peerReviewVoterModel.FVoteIP = null;
                    peerReviewVotorModelList.Add(peerReviewVoterModel);

                    tableJsonStr += hrEmplBLL.GetEmpNameAndDept(entityNo, selectItem.Value) + ",";
                }
                //保存
                try
                {
                    peerReviewVoterBll.Add(peerReviewVotorModelList, decimal.Parse(peerReviewID));
                }
                catch (Exception)
                {

                }
                if (itemList.Count > 0)
                {
                    tableJsonStr = tableJsonStr.Substring(0, tableJsonStr.LastIndexOf(","));
                }

            }
            tableJsonStr += "]";

            return tableJsonStr;
        }

    }
}
